<?php

namespace Home\Controller;
use Think\Controller;
class IndexController extends Controller{

    public function __construct(){
            parent::__construct();
            $sid = session('adminId');
            //var_dump($sid); 打印session类型
            if($sid!=''){ //未登录时空字符串
                $member=M("users")->where('phone='.$sid)->find();
            }
            $config=M("setting")->where("id<>''")->order("id asc")->select();
            $menu=M("menu")->where("id<>''")->order("sort asc")->select();
            $this->assign("config" ,$config); 
            $this->assign("title",$config[1]['value']); 
            $this->assign("keyword",$config[2]['value']); 
            $this->assign("description",$config[3]['value']); 
            $this->assign("phone",$config[4]['value']); 
            $this->assign("beian",$config[5]['value']); 
            $this->assign("copyright",$config[6]['value']); 
            $this->assign("menu",$menu); 
      $this->assign("member",$member); 
    }
    

    //首页方法
    public function index(){
    $banner=M("slide")->where("status=1")->order("id asc")->limit(5)->select();
    $h_rent=M("rent")->where('zulinfs="合租"')->order("id desc")->limit(3)->select();
    $h_rent1=M("rent")->where('zulinfs="合租" and recommend=2')->order("id desc")->limit(1)->select();
    $t_rent=M("rent")->where('zulinfs="整租"')->order("id desc")->limit(3)->select();
    $t_rent1=M("rent")->where('zulinfs="整租" and recommend=2')->order("id desc")->limit(1)->select();
    $z_rent=M("rent")->where('zulinfs="直租"')->order("id desc")->limit(3)->select();
    $this->assign("h_rent",$h_rent); 
    $this->assign("h_rent1",$h_rent1); 
    $this->assign("t_rent",$t_rent);
    $this->assign("t_rent1",$t_rent1); 
    $this->assign("z_rent",$z_rent); 
    $this->assign("banner",$banner); 
    $this->display();
    }

    //好房东加盟方法
    public function league()
    {
      $this->display();
    }
    

    
    //服务指南方法
    public function serviceBook()
    {
        $this->display();
    }
    
    public function service(){
        $titleWord = $_POST['titleWord'];
        $data = M('service')->where('title="'.$titleWord.'"')->find();
//      print_r($data) ;
        echo htmlspecialchars_decode($data['content']);
    }
    

    //关于好房东方法
    public function aboutus()
    {
      $this->display();
    }
    
    
    public function rentcontent(){
    $id=I('get.id');    
    
    $content_id = array();//定义一个空数组
    $content_id[] = $id; //将接受过来的值放入数组中
    if(isset($_COOKIE['content_id'])) //判定cookie是否存在
    {
    $now_content = str_replace("", "", $_COOKIE['content_id']);//您可以查看下cookie,此时如果unserialize的话出问题的,我把里面的斜杠去掉了 
    $now = unserialize($now_content); //把cookie 中的serialize生成的字符串反实例化成数组 
    foreach($now as $n=>$w) { //里面很多元素,所以我要foreach 出值 
    if(!in_array($w,$content_id)) //判定这个值是否存在,如果存在的化我就不插入到数组里面去; 
    { 
    $content_id[] = $w; //插入到数组 
    } 
    } 
    $content= serialize($content_id); //把数组实例化成字符串 
    setcookie("content_id",$content, time()+3600*24); //插入到cookie
    }else { 
    $content= serialize($content_id);//把数组实例化成字符串 
    setcookie("content_id",$content, time()+3600*24); //生成cookie
    }
    $getcontent = unserialize(str_replace("", "", $_COOKIE['content_id'])); 
//  foreach($getcontent as $row=>$r) 
//  { 
//  echo $r;//(取值) 
//  }
    
    if($_COOKIE['content_id']){
              $condition['flag']=1; 
              $condition['id']=array('in',$getcontent); 
              $condition['_logic']='and'; 
              $tuijian=M("rent")->where($condition)->select();  
    }
    else{
         $tuijian=M("rent")->where('flag=1')->order("id desc")->select();      
    }
    

    

    $content=M("rent")->where('id="'.$id.'"')->find();
    $pz=explode("|",rtrim($content['peizhi'], '|'));
    $ts=explode("|",rtrim($content['tese'], '|'));
    $imgurl_list=M("imgurl_list")->where("rentid='".$id."'")->select();
    $imgurl_ct=M("imgurl_ct")->where("rentid='".$id."'")->select();
    $imgurl_housetype=M("imgurl_housetype")->where("rentid='".$id."'")->select();
    $imgurl_ht_thumb=M("imgurl_housetype_thumb")->where("rentid='".$id."'")->select();
    $fenrent = M('fenrent')->where('rel_id="'.$id.'"')->select();
    $price_img = M("price_img")->where("rentid=".$id)->find();
    $fenrenta = M('fenrent')->field('imgurl,fanghao')->where('rel_id="'.$id.'" and fanghao="房间A"')->find(); 
    $fenrentb = M('fenrent')->field('imgurl,fanghao')->where('rel_id="'.$id.'" and fanghao="房间B"')->find(); 
    $fenrentc = M('fenrent')->field('imgurl,fanghao')->where('rel_id="'.$id.'" and fanghao="房间C"')->find(); 
    $fenrentd = M('fenrent')->field('imgurl,fanghao')->where('rel_id="'.$id.'" and fanghao="房间D"')->find(); 
    $nb=M("rent")->where('flag=1')->order("id desc")->limit(5)->select();   
    $nearby=M('rent')->where("area='".$content['area']."' AND id<>".$id)->select();
    $getLike=M('rent')->where("area='".$content['area']."' AND id<>".$id)->select();
    $this->assign('nearby',$nearby);
    $this->assign('getLike',$getLike);
    $this->assign('housetitle',$content['title']);
    $this->assign('houseid',$id);
    $this->assign('nb',$nb);                
    $this->assign('fenrenta', $fenrenta);
    $this->assign('imgurl_list',$imgurl_list);
    $this->assign('imgurl_ct',$imgurl_ct);
    $this->assign('imgurl_housetype',$imgurl_housetype);
    $this->assign('imgurl_ht_thumb',$imgurl_ht_thumb);
    $this->assign('pz',$pz);
    $this->assign('ts',$ts);
    $this->assign('fenrentb', $fenrentb);
    $this->assign('fenrentc', $fenrentc);
    $this->assign('fenrentd', $fenrentd);
    $this->assign('content', $content);
    $this->assign('fenrent', $fenrent);
    $this->assign('fenrentView', $fenrent);
    $this->assign('tuijian', $tuijian);
    $this->assign('price_img',$price_img['imgurl']);
    $this->display();
    }
    
    
    //首页搜索方法
    public function search($key="")
    {
        if($key === ""){
            $model = M('rent'); 

        }else{
            $where['area'] = array('like',"%$key%");//匹配区域
            $where['tube'] = array('like',"%$key%");//匹配地铁
            $where['_logic'] = 'or';
            $model = M('rent')->where($where); 
        }
        $count  = $model->where($where)->count();// 查询满足要求的总记录数
        $Page = new \Extend\Pageqt($count,6);// 实例化分页类 传入总记录数和每页显示的记录数(25)
        $show = $Page->show();// 分页显示输出
        $pages = $model->limit($Page->firstRow.','.$Page->listRows)->where($where)->order('id DESC')->select();
        $this->assign('model',$pages);
        $this->assign('page',$show);
        $this->display();     
    }  
    
    
    //好房东房源ajax请求
    public function rentajax(){ 
        $conditions = array('sortType','rentType','area','price','room','square','direction','page');
        $sortType = $rentType = $area = $price = $room = $square = $direction='';
        $arr=$_POST;
        $page=$arr['page'];
        if($_POST['rentType']){
        if($_POST['rentType']==0){
//      $condition['zulinfs']=array('in',array('合租')); 
        }
        elseif($_POST['rentType']==1){
        $condition['zulinfs']=array('in',array('合租')); 
        }
        elseif($_POST['rentType']==2){
        $condition['zulinfs']=array('in',array('直租')); 
        }
        }
            
        if($_POST['area']){
        $condition['area']=array('in',$_POST['area']); 
        }
        
        if($_POST['price']){
        $condition['price_area']=array('in',$_POST['price']); 
        }
        
        if($_POST['room']){
        $condition['huxing']=array('in',$_POST['room']); 
        }
        
        if($_POST['square']){
        $condition['mianji_area']=array('in',$_POST['square']); 
        }
        
        if($_POST['direction']){
        $condition['chaoxiang']=array('in',$_POST['direction']); 
        }

                    $condition['flag']=1; 
                    $condition['_logic']='and'; 
                    
                    if($_POST['sortType']){
                    if($_POST['sortType']==1){
                    $bianl='price desc';
                    }
                    elseif($_POST['sortType']==2){
                    $bianl='mianji desc';
                    }
                    }
                    else{
                    $bianl='id desc';
        }
        $start=$page*10-10;
        $data=M("rent")->field('imgurl,title,mianji,huxing,floor,zulinfs,dizhi,peizhi,price,id')->where($condition)->order($bianl)->limit($start,10)->select();
        //$data['peizhi']=explode("|",$data['peizhi']);
        $count=M("rent")->where($condition)->count();
        $data=array("count" => $count , "datas" => $data);
        $mm=json_encode($data);
        $this->ajaxReturn($mm,'JSON');


 
}
    
     
   
    public function rent(){
      $arrlist=$_POST['list'];
      $model =M('rent'); 
      $rent_list = M("rent")->where('zulinfs="合租"')->order("id desc")->select();
      $count  = $model->where($where)->count();// 查询满足要求的总记录数
      $Page = new \Extend\Pageqt($count,10);// 实例化分页类 传入总记录数和每页显示的记录数
      $show = $Page->show();// 分页显示输出
      $pages = $model->limit($Page->firstRow.','.$Page->listRows)->where($where)->order($order)->select();  
      $this->assign('rent_list',$rent_list);
      $this->assign('list', $pages);
      $this->assign('page',$show);
      $this->display();
    }

    public function yuyue(){
        $data['housetitle']=$_POST['title'];
        $data['room']=$_POST['room'];
        $data['name']=$_POST['name'];
        $data['tel']=$_POST['mobile'];
        $data['sex']=$_POST['sex'];
        $data['owner']=$_POST['owner'];
        $data['date']=$_POST['date'];
        //$data['time']=$_POST['time'];
        $data['textarea']=$_POST['need'];
        $data['houseid']=$_POST['num'];
        $data['addtime']=time();
        $res = M('hfd_yuyue')->data($data)->add();
          $resSql     = M('hfd_yuyue');
        //echo $resSql->getLastSql();
        if($res){
            $this->ajaxReturn("1","JSON");
        }else{
            $this->ajaxReturn("0","JSON");
        }
    }
    

   
 }   
         